package com.esri.core.map.popup;

import com.esri.core.internal.util.d;
import com.esri.core.io.EsriServiceException;
import com.esri.core.map.AttachmentInfo;
import com.esri.core.map.Graphic;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.achartengine.internal.a;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class PopupInfo {
    private PopupMediaInfo[] a;
    boolean d;
    protected String description;
    AttachmentInfo[] e;
    double f;
    protected LinkedHashMap<String, PopupFieldInfo> fieldInfos;
    protected double minScale;
    protected String title;

    public PopupInfo(PopupInfo popupInfo) {
        LinkedHashMap<String, PopupFieldInfo> linkedHashMap = new LinkedHashMap<>();
        this.fieldInfos = linkedHashMap;
        this.title = popupInfo.title;
        this.description = popupInfo.description;
        linkedHashMap.putAll(popupInfo.getFieldInfos());
        this.minScale = popupInfo.minScale;
        this.d = popupInfo.d;
        this.a = popupInfo.a;
        this.e = popupInfo.e;
    }

    public PopupInfo(Graphic graphic) {
        String[] attributeNames;
        this.fieldInfos = new LinkedHashMap<>();
        if (graphic != null) {
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            for (String str : graphic.getAttributeNames()) {
                PopupFieldInfo popupFieldInfo = new PopupFieldInfo();
                popupFieldInfo.setFieldName(str);
                popupFieldInfo.setLabel(str);
                linkedHashMap.put(str, popupFieldInfo);
            }
            setFieldInfos(linkedHashMap);
        }
    }

    public boolean isShowAttachments() {
        return this.d;
    }

    public void setShowAttachments(boolean z) {
        this.d = z;
    }

    public double getMinScale() {
        return this.minScale;
    }

    public void setMinScale(double d) {
        this.minScale = d;
    }

    public double getMaxScale() {
        return this.f;
    }

    public void setMaxScale(double d) {
        this.f = d;
    }

    public PopupInfo() {
        this.fieldInfos = new LinkedHashMap<>();
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String str) {
        this.description = str;
    }

    public Map<String, PopupFieldInfo> getFieldInfos() {
        return new LinkedHashMap(this.fieldInfos);
    }

    public PopupFieldInfo getFieldInfo(String str) {
        return this.fieldInfos.get(str);
    }

    public void setFieldInfos(Map<String, PopupFieldInfo> map) {
        this.fieldInfos.putAll(map);
    }

    public PopupMediaInfo[] getMediaInfos() {
        return this.a;
    }

    public void setMediaInfos(PopupMediaInfo[] popupMediaInfoArr) {
        this.a = popupMediaInfoArr;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("PopupInfo [title=");
        sb.append(this.title);
        sb.append(", description=");
        sb.append(this.description);
        sb.append(", fieldInfos=");
        LinkedHashMap<String, PopupFieldInfo> linkedHashMap = this.fieldInfos;
        sb.append(linkedHashMap != null ? a(linkedHashMap.entrySet(), 3) : null);
        sb.append(", minScale=");
        sb.append(this.minScale);
        sb.append(", showAttachments=");
        sb.append(this.d);
        sb.append(", mediaInfos=");
        PopupMediaInfo[] popupMediaInfoArr = this.a;
        sb.append(popupMediaInfoArr != null ? Arrays.asList(popupMediaInfoArr).subList(0, Math.min(this.a.length, 3)) : null);
        sb.append(", attachmentInfos=");
        AttachmentInfo[] attachmentInfoArr = this.e;
        sb.append(attachmentInfoArr != null ? Arrays.asList(attachmentInfoArr).subList(0, Math.min(this.e.length, 3)) : null);
        sb.append(", maxScale=");
        sb.append(this.f);
        sb.append("]");
        return sb.toString();
    }

    public static PopupInfo fromJson(JsonParser jsonParser) throws Exception {
        if (!d.c(jsonParser)) {
            throw new EsriServiceException("This response cannot be parsed.");
        }
        PopupInfo popupInfo = new PopupInfo();
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            JsonToken nextToken = jsonParser.nextToken();
            if (a.b.equals(currentName)) {
                popupInfo.setTitle(jsonParser.getText());
            } else if ("description".equals(currentName)) {
                popupInfo.setDescription(jsonParser.getText());
            } else if ("fieldInfos".equals(currentName) && !nextToken.equals(JsonToken.VALUE_NULL)) {
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    PopupFieldInfo fromJson = PopupFieldInfo.fromJson(jsonParser);
                    popupInfo.fieldInfos.put(fromJson.getFieldName(), fromJson);
                }
            } else if ("showAttachments".equals(currentName)) {
                popupInfo.d = jsonParser.getBooleanValue();
            } else if ("mediaInfos".equals(currentName)) {
                ArrayList arrayList = new ArrayList();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    arrayList.add(PopupMediaInfo.fromJson(jsonParser));
                }
                popupInfo.setMediaInfos((PopupMediaInfo[]) arrayList.toArray(new PopupMediaInfo[0]));
            } else {
                jsonParser.skipChildren();
            }
        }
        return popupInfo;
    }

    public String toJson() throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        String str = this.title;
        if (str != null) {
            a.writeStringField(a.b, str);
        }
        String str2 = this.description;
        if (str2 != null) {
            a.writeStringField("description", str2);
        }
        if (this.fieldInfos != null) {
            a.writeArrayFieldStart("fieldInfos");
            for (PopupFieldInfo popupFieldInfo : this.fieldInfos.values()) {
                a.writeRawValue(popupFieldInfo.toJson());
            }
            a.writeEndArray();
        }
        a.writeBooleanField("showAttachments", this.d);
        if (this.a != null) {
            a.writeArrayFieldStart("mediaInfos");
            for (PopupMediaInfo popupMediaInfo : this.a) {
                a.writeRawValue(popupMediaInfo.toJson());
            }
            a.writeEndArray();
        }
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    private String a(Collection<?> collection, int i) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Iterator<?> it = collection.iterator();
        for (int i2 = 0; it.hasNext() && i2 < i; i2++) {
            if (i2 > 0) {
                sb.append(", ");
            }
            sb.append(it.next());
        }
        sb.append("]");
        return sb.toString();
    }
}
